require ('./common.js');
require ('./config.js');

var SerialPort = require("serialport");

global.SerialPort_Read = new SerialPort("/dev/pts/1", global.SerialPortConfig);

global.SerialPort_Read.open(function(error){
  if(error){
    console.log("打开窗口"+portName+"错误："+error);
  }else{ 
    console.log("打开窗口成功，正在监听数据中...");
  }
});

global.SerialPort_Read.on( "data", function( data ) {
  data = +data;
  console.log("接收到串口数据:"+data);
});

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////

global.SerialPort_Send = new SerialPort("/dev/pts/2", global.SerialPortConfig);

global.SerialPort_Send.open(function(error){
  if(error){
    console.log("打开端口"+portName+"错误："+error);
  }else{ 
    console.log("打开端口成功，准备发送数据...");
  }
});

global.SerialPort_writeAndDrain = function(data,callback) {
  global.SerialPort_Send.write(data, function () {
    global.SerialPort_Send.drain(callback);
  });
}



